(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        define(['exports'], factory);
    } else if (typeof module === 'object' && module.exports) {
        module.exports = factory(exports);
    } else {
        root.LogRefDecoder = factory();
    }
}(typeof self !== 'undefined' ? self : this, function () {
    var LogRefDecoder = function(prevNode, prefixLen, suffixLen) {
        this.prevNode = prevNode;
        this.prefixLen = prefixLen;
        this.suffixLen = suffixLen;
        // console.log("this.prevNode:", this.prevNode, "this.prefixLen:", this.prefixLen, "this.suffixLen:", this.suffixLen);
    }
    LogRefDecoder.prototype = {}

    /**
     * Inflates the Kaitai stream.
     * @param suffixBytes The data to inflate. If there is no data and a stream was provided in the constructor, the stream is used instead.
     * @returns The inflated data.
     */
    LogRefDecoder.prototype.decode = function(suffixBytes) {
        // console.log("suffixBytes: ", suffixBytes);
        
        let prefixBytes = new Uint8Array([]);
        if (this.prevNode && this.prefixLen > 0) {
            prefixBytes = this.prevNode.refname.fullBytes.slice(0, this.prefixLen);
            // console.log("prefix: ", prefixBytes);
        }

        let fullBytes = new Uint8Array(prefixBytes.length + suffixBytes.length);
        fullBytes.set(prefixBytes, 0);
        fullBytes.set(suffixBytes, prefixBytes.length);
        // console.log("fullBytes: ", fullBytes);
        let fullname = (new TextDecoder()).decode(fullBytes.slice(0, fullBytes.length - 8));

        let updateIndexBytes = fullBytes.slice(fullBytes.length - 8, fullBytes.length);
        let updateIndex = new Long(0xFFFFFFFF, 0xFFFFFFFF) - Long.fromBytesBE(updateIndexBytes);
        return {                
            "fullBytes": fullBytes,
            "prefixBytes": prefixBytes,
            "prefixLen": this.prefixLen,
            "suffixBytes": suffixBytes,
            "suffixLen": this.suffixLen,
            "fullname": fullname,
            "updateIndexBytes": updateIndexBytes,
            "updateIndex": updateIndex,
        }
    }

    return LogRefDecoder;
}));
